from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.models import Model


class AdminLog(Model):
    id = fields.IntField(pk=True)
    method = fields.CharField(max_length=10)
    uid = fields.IntField()
    url = fields.CharField(max_length=255)
    desc = fields.TextField(null=True)
    ip = fields.CharField(max_length=255)
    success = fields.IntField()
    user_agent = fields.TextField(null=True)
    create_time = fields.DatetimeField(auto_now_add=True)

    class Meta:
        table = "admin_admin_log"


# 登录日志表
class LoginLog(Model):
    """
    登录日志
    """

    id = fields.IntField(pk=True)
    user_id = fields.IntField(null=True, description="用户 ID")
    ip = fields.CharField(max_length=255)
    request_id = fields.CharField(max_length=64, description="请求ID")
    # 请求参数
    form = fields.TextField(description="请求参数", null=True)
    # 浏览器
    browser = fields.CharField(max_length=255, null=True, description="浏览器")
    # 系统
    system = fields.CharField(max_length=255, null=True, description="系统")
    # 状态
    status = fields.IntField(description="状态", default=1)
    country = fields.CharField(max_length=64, null=True, description="国家")
    region = fields.CharField(max_length=64, null=True, description="大区")
    province = fields.CharField(max_length=64, null=True, description="省份")
    city = fields.CharField(max_length=64, null=True, description="城市")
    isp = fields.CharField(max_length=128, null=True, description="运营商")
    remark = fields.CharField(max_length=255, null=True, description="备注")
    # 请求地址
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")

    class Meta:
        table = "admin_login_log"
        table_description = "管理员登录日志"


# 操作日志
class OperationLog(Model):
    """
    操作记录
    """

    id = fields.IntField(pk=True)
    method = fields.CharField(max_length=10, description="请求方式")
    ip = fields.CharField(max_length=255, description="ip地址", null=True)
    user_id = fields.IntField(description="用户编号", null=True)
    url = fields.CharField(max_length=255, description="请求地址")
    request_id = fields.CharField(max_length=64, description="请求ID")
    # 路由描述
    summary = fields.CharField(max_length=255, null=True, description="路由描述")
    # 请求参数
    params = fields.TextField(description="请求参数", null=True)
    # 耗时
    cost_time = fields.FloatField(description="耗时", null=True)
    # 浏览器
    browser = fields.CharField(max_length=255, null=True, description="浏览器")
    # 系统
    system = fields.CharField(max_length=255, null=True, description="系统")
    # 标签
    tags = fields.CharField(max_length=255, description="标签", null=True)
    # 路由名称
    route_name = fields.CharField(max_length=255, description="路由名称", null=True)
    # 状态
    status = fields.IntField(description="状态", default=1)
    content_length = fields.CharField(max_length=32, description="内容长度", null=True)
    country = fields.CharField(max_length=64, null=True, description="国家")
    region = fields.CharField(max_length=64, null=True, description="大区")
    province = fields.CharField(max_length=64, null=True, description="省份")
    city = fields.CharField(max_length=64, null=True, description="城市")
    isp = fields.CharField(max_length=128, null=True, description="运营商")
    # 请求地址
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")

    class Meta:
        table = "admin_operation_log"


OperationLogOut = pydantic_model_creator(OperationLog)

LogOutSchema = pydantic_model_creator(LoginLog)
